package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.EvaluationProject;
import cn.jdy.plugins.service.EvaluationProjectService;
import cn.jdy.plugins.service.LiveService;

/**
 * @desc   测评项目管理控制操作类
 * @author nignqian
 * @date   2016-7-4
 */
@Controller
@RequestMapping("/evaluationProject")
public class EvaluationProjectCtrl extends BaseCtrl {
	@Autowired
	private EvaluationProjectService evaluationProjectService;
	@Autowired
	private LiveService liveService;
	//默认列表路径
	private static String viewName = "redirect:/evaluationProject/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list( EvaluationProject entity, String fromDate, String toDate, Long liveId, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/evaluationManage/evaluationProjectList.jsp");
		
		mv.addObject("iTotalRecords", evaluationProjectService.count(entity, fromDate, toDate));
		mv.addObject("liveId", liveId);
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}
	
	
	
	
	/**
	 * 异步查询测评项目信息
	 * @param  evaluationProject
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(EvaluationProject evaluationProject, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", evaluationProjectService.query(evaluationProject, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", evaluationProjectService.count(evaluationProject, fromDate, toDate));
			jsonMap.put("iTotalRecords", evaluationProjectService.count(evaluationProject, fromDate, toDate));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}	
		
	

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(EvaluationProject p) {
		ModelAndView mv = new ModelAndView("/evaluationManage/evaluationProjectDetail.jsp");
	
		if(p.getLiveId()!=null){
		mv.addObject("datalists", liveService.find("id",p.getLiveId()));
		}else{
		mv.addObject("datalists", liveService.findAll());
		}

		mv.addObject("data", null== p.getId() || 0 ==  p.getId() ?  p.getId() : this.evaluationProjectService.find( p.getId()));
		return mv;
	}

	 

	/**
	 * 保存（更新）测评项目信息
	 * @param evaluationProject
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public ModelAndView save(EvaluationProject evaluationProject) {
		ModelAndView mv = new ModelAndView();
		try {
			if(null == evaluationProject.getId()) {
				//新增
				evaluationProject.setInsertTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.evaluationProjectService.save(evaluationProject);
				
				mv.setViewName(this.viewName.concat("1"));
				mv.setViewName("redirect:/evaluationProject/list?liveId="+evaluationProject.getLiveId());

			} else {
				//修改
				evaluationProject.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.evaluationProjectService.update(evaluationProject);
				mv.setViewName(this.viewName.concat("2"));

				mv.setViewName("redirect:/evaluationProject/list?liveId="+evaluationProject.getLiveId());
		    // 清理缓存
		    NginxPurgeCacheUtil.purge(2, evaluationProject.getId());
			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	
	/**
	 * 删除测评项目信息
	 * @return
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public Map<String, Object>  del(@PathVariable Long id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			this.evaluationProjectService.delete(id);
			jsonMap.put("msg", "成功");
			// 清理缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			jsonMap.put("msg", "失败");
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return jsonMap;
	}

}
